<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据编号" prop="fnumber">
        <el-input
          v-model="queryParams.fnumber"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="薪酬期间" prop="fperiod">
        <el-input
          v-model="queryParams.fperiod"
          placeholder="请输入薪酬期间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组织" prop="fcompany">
        <el-input
          v-model="queryParams.fcompany"
          placeholder="请输入组织"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="fstate">
        <el-select v-model="queryParams.fstate" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.bill_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="来源单ID" prop="fsourcebillid">
        <el-input
          v-model="queryParams.fsourcebillid"
          placeholder="请输入来源单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-view"
          size="mini"
          :disabled="single"
          @click="handleView"
        >查看</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basebill:xczbsb:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basebill:xczbsb:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basebill:xczbsb:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basebill:xczbsb:export']"
        >导出</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-dropdown class="avatar-container" trigger="click" :disabled="multiple">
          <div class="avatar-wrapper">
            <el-button
              type="primary"
              size="mini"
              plain
              :disabled="multiple"
            >
              单据操作
              <i class="el-icon-bottom" />
            </el-button>
          </div>
          <el-dropdown-menu slot="dropdown" class="user-dropdown" divided>
            <a>
              <el-dropdown-item @click.native="handleAudit">
                审核
              </el-dropdown-item>
            </a>
            <a>
              <el-dropdown-item lot="dropdown" class="user-dropdown" divided @click.native="handleDeAudit">
                反审核
              </el-dropdown-item>
            </a>
            <a>
              <el-dropdown-item lot="dropdown" class="user-dropdown" divided @click.native="handleDisuse">
                废弃
              </el-dropdown-item>
            </a>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
      <el-col :span="1.5">
        <el-dropdown class="avatar-container" trigger="click">
          <div class="avatar-wrapper">
            <el-button
              type="primary"
              size="mini"
              plain
              :disabled="single"
            >
              下推
              <i class="el-icon-bottom" />
            </el-button>
          </div>
          <el-dropdown-menu slot="dropdown" class="user-dropdown" divided>
            <a>
              <el-dropdown-item lot="dropdown" class="user-dropdown" divided @click.native="handleSbcl">
                社保处理
              </el-dropdown-item>
            </a>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="xczbsbList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="fid" />
      <el-table-column label="单据编号" align="center" prop="fnumber" />
      <el-table-column label="薪酬期间" align="center" prop="fperiod" />
      <el-table-column label="组织" align="center" prop="fcompany" />
      <el-table-column label="状态" align="center" prop="fstate">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bill_state" :value="scope.row.fstate"/>
        </template>
      </el-table-column>
      <el-table-column label="来源单ID" align="center" prop="fsourcebillid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basebill:xczbsb:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basebill:xczbsb:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改薪酬总表-当月社保数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="100%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="8">
        <el-form-item label="单据编号" prop="fnumber">
          <el-input v-model="form.fnumber" placeholder="请输入单据编号" />
        </el-form-item>
          </el-col>
          <el-col :span="8">
        <el-form-item label="薪酬期间" prop="fperiod">
          <el-input v-model="form.fperiod" placeholder="请输入薪酬期间" />
        </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="组织" prop="fcompany">
              <el-input v-model="form.fcompany" placeholder="请输入组织" />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="状态" prop="fstate">
              <el-select v-model="form.fstate" placeholder="请选择状态">
                <el-option
                  v-for="dict in dict.type.bill_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
        <el-form-item label="来源单ID" prop="fsourcebillid">
          <el-input v-model="form.fsourcebillid" placeholder="请输入来源单ID" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">薪酬总表-当月社保数据分录信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddWfzXczbsbentry">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteWfzXczbsbentry">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="wfzXczbsbentryList" :row-class-name="rowWfzXczbsbentryIndex" @selection-change="handleWfzXczbsbentrySelectionChange" ref="wfzXczbsbentry">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="成本中心" prop="fcostnumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fcostnumber" placeholder="请输入成本中心" />
            </template>
          </el-table-column>
          <el-table-column label="成本中心名称" prop="fcostname" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fcostname" placeholder="请输入成本中心名称" />
            </template>
          </el-table-column>
          <el-table-column label="成本中心所属财务组织" prop="fcostcompany" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fcostcompany" placeholder="请输入成本中心所属财务组织" />
            </template>
          </el-table-column>
          <el-table-column label="员工编号" prop="fpersonnumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fpersonnumber" placeholder="请输入员工编号" />
            </template>
          </el-table-column>
          <el-table-column label="应发工资" prop="fyfgz" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fyfgz" placeholder="请输入应发工资" />
            </template>
          </el-table-column>
          <el-table-column label="养老保险" prop="fylbx" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fylbx" placeholder="请输入养老保险" />
            </template>
          </el-table-column>
          <el-table-column label="医疗保险" prop="fyybx" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fyybx" placeholder="请输入医疗保险" />
            </template>
          </el-table-column>
          <el-table-column label="失业保险" prop="fsybx" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fsybx" placeholder="请输入失业保险" />
            </template>
          </el-table-column>
          <el-table-column label="工伤保险" prop="fgsbx" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fgsbx" placeholder="请输入工伤保险" />
            </template>
          </el-table-column>
          <el-table-column label="生育保险" prop="fsyubx" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fsyubx" placeholder="请输入生育保险" />
            </template>
          </el-table-column>
          <el-table-column label="个人养老" prop="fgryl" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fgryl" placeholder="请输入个人养老" />
            </template>
          </el-table-column>
          <el-table-column label="个人医疗" prop="fgryy" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fgryy" placeholder="请输入个人医疗" />
            </template>
          </el-table-column>
          <el-table-column label="个人失业" prop="fgrsy" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fgrsy" placeholder="请输入个人失业" />
            </template>
          </el-table-column>
          <el-table-column label="补扣养老保险" prop="fbkylbx" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fbkylbx" placeholder="请输入补扣养老保险" />
            </template>
          </el-table-column>
          <el-table-column label="补扣医疗保险" prop="fbkyybx" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fbkyybx" placeholder="请输入补扣医疗保险" />
            </template>
          </el-table-column>
          <el-table-column label="补扣失业保险" prop="fbksybx" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fbksybx" placeholder="请输入补扣失业保险" />
            </template>
          </el-table-column>
          <el-table-column label="补扣工伤保险" prop="fbkgsbx" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fbkgsbx" placeholder="请输入补扣工伤保险" />
            </template>
          </el-table-column>
          <el-table-column label="补扣生育保险" prop="fbksyubx" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fbksyubx" placeholder="请输入补扣生育保险" />
            </template>
          </el-table-column>
          <el-table-column label="银行开户行" prop="fbankname" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fbankname" placeholder="请输入银行开户行" />
            </template>
          </el-table-column>
          <el-table-column label="是否离职" prop="fislz" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.fislz" placeholder="请选择是否离职">
                <el-option
                  v-for="dict in dict.type.yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="Number(dict.value)"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="是否当月缴纳" prop="fissbdy" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.fisdy" placeholder="请选择是否当月缴纳">
                <el-option
                  v-for="dict in dict.type.yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="Number(dict.value)"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="社保缴纳单位" prop="fsbdw" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fsbdw" placeholder="请输入社保缴纳单位" />
            </template>
          </el-table-column>
          <el-table-column label="凭证字" prop="fvourtype" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fvourtype" placeholder="请输入凭证字" />
            </template>
          </el-table-column>
          <el-table-column label="组织" prop="fcompany" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fcompany" placeholder="请输入组织" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listXczbsb, getXczbsb, delXczbsb, addXczbsb, updateXczbsb } from "@/api/basebill/xczbsb";
import { handleAudit, handleDeAudit, handleDisuse,operation} from "@/api/basebill/xczbsb";

export default {
  name: "Xczbsb",
  dicts: ['bill_state','yes_no','cost_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWfzXczbsbentry: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 薪酬总表-当月社保数据表格数据
      xczbsbList: [],
      // 薪酬总-当月社保数据分录表格数据
      wfzXczbsbentryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示控件
      show: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fnumber: null,
        fperiod: null,
        fcompany: null,
        fstate: null,
        fsourcebillid: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询薪酬总表-当月社保数据列表 */
    getList() {
      this.loading = true;
      listXczbsb(this.queryParams).then(response => {
        this.xczbsbList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fid: null,
        fnumber: null,
        fperiod: null,
        fcompany: null,
        fcreator: null,
        fcreatetime: null,
        flastupdateuser: null,
        flastupdatetime: null,
        fstate: null,
        fsourcebillid: null
      };
      this.wfzXczbsbentryList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fid)
      this.states = selection.map(item => item.fstate)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const fid = row.fid || this.ids
      getXczbsb(fid).then(response => {
        this.form = response.data;
        this.wfzXczbsbentryList = response.data.wfzXczbsbentryList;
        this.open = true;
        this.show = true;
        this.title = "查看薪酬总表-当月社保数据";
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.show = false;
      this.title = "添加薪酬总表-当月社保数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fid = row.fid || this.ids
      getXczbsb(fid).then(response => {
        this.form = response.data;
        this.wfzXczbsbentryList = response.data.wfzXczbsbentryList;
        this.open = true;
        this.show = false;
        this.title = "修改薪酬总表-当月社保数据";
      });
    },
    /** 审核处理操作 */
    handleAudit(row) {
      const fids = row.fid || this.ids;
      const fstate = "AUDIT";
      var state = this.states;
      var that = this;
      this.$modal.confirm('是否确认审核薪酬总表编号为"' + fids + '"的数据项').then(function() {
        for (var i = 0; i < state.length; i++) {
          console.log("开始遍历")
          if(state[i]!="SAVE"){
            console.log("准备报错")
            that.$modal.msgError("您选中的单据状态不为保存状态，只有保存状态下的单据可以审核!");
            console.log("err")
            return false;
          }
        }
        return operation(fids,fstate).then(() => {
          console.log("状态更改结束")
          that.getList();
          that.$modal.msgSuccess("审核成功");
        })
        console.log(fids+"审核处理操作")
      }).catch(() => {});
    },

    /** 反审核处理操作 */
    handleDeAudit(row) {
      const fids = row.fid || this.ids;
      const fstate = "SAVE";
      var state = this.states;
      var that = this;
      this.$modal.confirm('是否确认反审核薪酬总表编号为"' + fids + '"的数据项').then(function() {
        for (var i = 0; i < state.length; i++) {
          if(state[i]!="AUDIT"){
            that.$modal.msgError("您选中的单据状态不为审核状态，只有审核状态下的单据可以反审核!");
            return false;
          }
        }
        return operation(fids,fstate).then(() => {
          that.getList();
          that.$modal.msgSuccess("反审核成功");
        })
        // console.log(fids+"反审核处理操作")
      }).catch(() => {});
    },

    /** 废弃处理操作 */
    handleDisuse(row) {
      const fids = row.fid || this.ids;
      const fstate = "REVOKE";
      var state = this.states;
      var that = this;
      this.$modal.confirm('是否确认废弃薪酬总表编号为"' + fids + '"的数据项').then(function() {
        for (var i = 0; i < state.length; i++) {
          if(state[i]!="SAVE"){
            that.$modal.msgError("您选中的单据状态不为保存状态，只有保存状态下的单据可以废弃!");
            return false;
          }
        }
        return operation(fids,fstate).then(() => {
          that.getList();
          that.$modal.msgSuccess("废弃成功");
        })
        // console.log(fids+"废弃处理操作")
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.wfzXczbsbentryList = this.wfzXczbsbentryList;
          if (this.form.fid != null) {
            updateXczbsb(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addXczbsb(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fids = row.fid || this.ids;
      this.$modal.confirm('是否确认删除薪酬总表-当月社保数据编号为"' + fids + '"的数据项？').then(function() {
        return delXczbsb(fids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 社保处理操作 */
    handleSbcl(row) {
      const fids = row.fid || this.ids;
      this.$modal.confirm('是否确认薪酬总表编号为"' + fids + '"的数据项下推生成社保相关单据').then(function() {
        // return delRemuneration(fids);
        console.log(fids+"社保处理操作")
      }).then(() => {
        // this.getList();
        // this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 薪酬总-当月社保数据分录序号 */
    rowWfzXczbsbentryIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 薪酬总-当月社保数据分录添加按钮操作 */
    handleAddWfzXczbsbentry() {
      let obj = {};
      obj.fcostnumber = "";
      obj.fcostname = "";
      obj.fcostcompany = "";
      obj.fpersonnumber = "";
      obj.fyfgz = "";
      obj.fylbx = "";
      obj.fyybx = "";
      obj.fsybx = "";
      obj.fgsbx = "";
      obj.fsyubx = "";
      obj.fgryl = "";
      obj.fgryy = "";
      obj.fgrsy = "";
      obj.fbkylbx = "";
      obj.fbkyybx = "";
      obj.fbksybx = "";
      obj.fbkgsbx = "";
      obj.fbksyubx = "";
      obj.fbankname = "";
      obj.fislz = "";
      obj.fisdy = "";
      obj.fsbdw = "";
      obj.fvourtype = "";
      obj.fcompany = "";
      this.wfzXczbsbentryList.push(obj);
    },
    /** 薪酬总-当月社保数据分录删除按钮操作 */
    handleDeleteWfzXczbsbentry() {
      if (this.checkedWfzXczbsbentry.length == 0) {
        this.$modal.msgError("请先选择要删除的薪酬总-当月社保数据分录数据");
      } else {
        const wfzXczbsbentryList = this.wfzXczbsbentryList;
        const checkedWfzXczbsbentry = this.checkedWfzXczbsbentry;
        this.wfzXczbsbentryList = wfzXczbsbentryList.filter(function(item) {
          return checkedWfzXczbsbentry.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleWfzXczbsbentrySelectionChange(selection) {
      this.checkedWfzXczbsbentry = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basebill/xczbsb/export', {
        ...this.queryParams
      }, `xczbsb_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
